/*
 * @Author: hongbin
 * @Date: 2022-12-24 20:25:00
 * @LastEditors: hongbin
 * @LastEditTime: 2022-12-24 20:56:27
 * @Description:设置请求结果 通过与否
 */
import { ObjectId } from "mongodb";
import { NextApiRequest, NextApiResponse } from "next";
import { VerifyFields } from "../../../src/api/utils";
import DB from "../mongodb";
import { receiveRequest } from "../utils";

export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
    if (!receiveRequest(req, res, "POST")) return;
    const db = await DB;
    console.log("设置请求结果", req.body);
    if (!VerifyFields.has(req.body, ["requestId", "result"])) {
        return res.status(500).json("缺少关键字段");
    }

    const update = await db.collection("requestList").updateOne(
        { _id: new ObjectId(req.body.requestId) },
        {
            $set: {
                result: req.body.result,
            },
        }
    );
    console.log(update);

    if (update.matchedCount) {
        res.status(200).json({ data: "成功" });
        return;
    }
    res.json({ data: null, msg: "失败", code: -1 });
}
